package com.goldcn.sys.service;

import com.goldcn.common.ResultData;
import com.goldcn.common.util.IdGen;
import com.goldcn.config.PropertiesConfig;
import com.goldcn.sys.dao.SysConfigMapper;
import com.goldcn.sys.dao.SysPaymentMapper;
import com.goldcn.sys.model.SysPayment;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Author Li manNa
 * mail: lmn_022@163.com
 * Date: 2017/7/22
 * Time: 17:21
 * To change this template use File | Settings | File Templates.
 */
@Service
@Transactional
public class SysPaymentService {

    @Autowired
    SysPaymentMapper sysPaymentMapper;

    @Autowired
    SysConfigMapper sysConfigMapper;

    @Autowired
    PropertiesConfig propertiesConfig;

    public ResultData getPaymentInfo(Long companyID){

        Map<String,Object> config = sysConfigMapper.selectName(companyID);

        List<SysPayment> defaultPayment = sysPaymentMapper.selectAllDefault(propertiesConfig.getPaymentCompanyID(),null);

        Map<String,Object> map = new HashMap();

        map.put("defaultPayment",defaultPayment);
        map.put("paymentList",sysPaymentMapper.selectAllCodeByCompanyID(companyID));
        map.put("sysConfig",config);

        ResultData resultData = new ResultData<>();
        resultData.setData(map);
        return resultData;
    }


    public ResultData update(SysPayment payment){
        ResultData resultData = new ResultData();

        SysPayment model = sysPaymentMapper.selectByPayCodeCompanyId(payment.getCompanyID(),payment.getPayCode());
        if(null!=model){
            //删除
            sysPaymentMapper.deleteByPrimaryKey(model.getPaymentID());
        }else{
            //新增
            SysPayment pay = sysPaymentMapper.selectByPrimaryKey(payment.getPaymentID());
            pay.setPaymentID(IdGen.get().nextId());
            pay.setAddTime(System.currentTimeMillis());
            pay.setUpdateTime(System.currentTimeMillis());
            pay.setCompanyID(payment.getCompanyID());
            sysPaymentMapper.insert(pay);
        }
        return resultData;
    }
}
